package agent;

import processing.core.PVector;

public class TestBehavior extends Behavior {
	float turnAngle;
	
	TestBehavior(Main p, float turnAngle) {
		super(p);
		this.turnAngle = turnAngle;
	}
	
	public void apply(Agent a){
		super.apply(a);
		updateVelocities(a);
		updatePositions(a);
	}
	
	public void updatePositions(Agent a) {
		PVector oldPosition = a.getCurrentPosition();
		PVector velocity = a.getCurrentVelocity();
		PVector[] positions = a.getPositions();
		for (int i = positions.length-1; i > 0; i--) {
			positions[i].set(positions[i-1]);
		}
		a.setPosition(0,PVector.add(oldPosition,velocity));
	}
	
	public void updateVelocities(Agent a) {
		float angle = p.random(-turnAngle, turnAngle);
		PVector newVelocity = new PVector();
		PVector oldVelocity = a.getCurrentVelocity();
		newVelocity.x = p.cos(angle)*oldVelocity.x - p.sin(angle)*oldVelocity.y;
		newVelocity.y = p.sin(angle)*oldVelocity.x + p.cos(angle)*oldVelocity.y;
		PVector[] velocities = a.getVelocities();
		for (int i = velocities.length-1; i > 1; i--) {
			velocities[i].set(velocities[i-1]);
		}
		a.setVelocity(newVelocity);
	}
	
	public float getTurnAngle() {
		return turnAngle;
	}
	
	public void setTurnAngle(float a) {
		turnAngle = a;
	}
}
